package fr.mehfile.view;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import fr.mehfile.R;
import fr.mehfile.api.database.DataBase;
import fr.mehfile.api.database.NewsFromDatabase;
import fr.mehfile.api.database.ProgramFromDatabase;
import fr.mehfile.api.database.ScheduleFromDatabase;
import fr.mehfile.api.parser.CurrentNews;
import fr.mehfile.view.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;

public class NewsList extends Activity implements View.OnClickListener  {
	

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.newslist);
		displayNewsList();
	}

	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
    // Affiche la liste des news
    private void displayNewsList() {

        NewsFromDatabase currentNews = null;

        DataBase database = DataBase.getInstance(this);
        ArrayList<NewsFromDatabase> newsFromDatabase = database.getAllNews();
        
        
        
        
		//Creation de la ArrayList qui nous permettra de remplire la listView
        ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
 
        //On declare la HashMap qui contiendra les informations pour un item
        HashMap<String, String> map;
         

        for (int i = 0; i < newsFromDatabase.size(); i++) {
        	currentNews = newsFromDatabase.get(i);
        	
        	
            map = new HashMap<String, String>();
            map.put("vignette", currentNews.getImageLocation());
            map.put("titre_news", currentNews.getTitle());
            map.put("date_news", currentNews.getDate());
            map.put("debut_article", currentNews.getContent());
            
                        
            listItem.add(map);
        	
        }
        
         
        
        
		//Creation d'un SimpleAdapter qui se chargera de mettre les items prŽsent dans notre list (listItem) dans la vue affichageitem
        SimpleAdapter newsAdapter = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.news_item_view,
               new String[] {"vignette","titre_news", "date_news", "debut_article"}, new int[] {R.id.vignette,R.id.titleNews,R.id.dateNews, R.id.contenueNews});
		
			
		ListView list_news = (ListView)findViewById(R.id.list_news);	
		list_news.setAdapter(newsAdapter);
		list_news.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(NewsList.this, News.class);
				intent.putExtra("newsId", arg2);
                startActivity(intent);
			}
		});
        
    }

}
